package com.jiukuaidao.library.net;

import android.text.TextUtils;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * Author : H_Joker
 * Date : 2016/5/13
 * Description :
 */
public abstract class ApiBaseBytesAbstract implements IApi {

    protected TreeMap<String, String> mParams = new TreeMap<>();

    protected RetroCallBackBytesImpl mRetroCallBackImp = new RetroCallBackBytesImpl();

    protected Call<ResponseBody> api;

    public ApiBaseBytesAbstract(MyRetroCallBytesBack myRetroCallBack) {
        mRetroCallBackImp.setCallBack(myRetroCallBack);
    }

    public ApiBaseBytesAbstract needBaseParams(boolean need) {
        if (need) {
            buildBaseParams();
        }
        return this;
    }

    @Override
    public void buildBaseParams() {

    }

    @Override
    public void aes() {

    }

    @Override
    public void start() {
        handParams();
        api = getCall();
        api.enqueue(mRetroCallBackImp);
    }

    private void handParams() {
        Iterator<Map.Entry<String, String>> entries = mParams.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            entry.setValue(String.valueOf(entry.getValue()).trim());
            entry.setValue(TextUtils.isEmpty(entry.getValue()) ? "" : entry.getValue());
            entry.setValue("null".equals(entry.getValue()) ? "" : entry.getValue());
        }
    }

    abstract public Call<ResponseBody> getCall();

}
